'use strict';

var home = angular.module("home", []);
home.controller("topCtrl", ['$scope', 'loginService', '$state', '$http',
  function ($scope, loginService, $state, $http) {
    $scope.nickname = loginService.user.nickname;
    $scope.logout = function () {
      $http.get(basePath + "logout.json")
        .success(function () {
          loginService.logout();
          $state.go("login")
        })
    };
    $.backstretch && $.backstretch("destroy", false);
    $scope.$on('$includeContentLoaded', function () {
      Layout.initHeader(); // init header
    });
  }]);

home.controller("sidebarCtrl", ['$scope', '$rootScope', '$http', 'menuService', '$state', '$stateParams',function ($scope,$rootScope, $http, menuService, $state, $stateParams) {
  $rootScope.menuHighlight = {};
  $rootScope.parentMenuHighlight = {};

  $http.get(basePath + "develop/menu/sidebar.json")
    .success(function (data) {
      $scope.level0List = data.returnData.level0List;
      $scope.level1Map = data.returnData.level1Map;
      $scope.level2Map = data.returnData.level2Map;
      menuService.menuMap = data.returnData.menuMap;
      if ($stateParams.menuId) {
        menuService.setCurrentMenu($stateParams.menuId);
      }
    });
  $scope.$on('$includeContentLoaded', function () {
    Metronic.init();
    Layout.initSidebar();// init sidebar
  });
  $scope.setCurrentMenuAndGo = function (isLeaf, currentMenuId, url) {
    if (!isLeaf)return;
    menuService.setCurrentMenu(currentMenuId);
    $state.go(url,{menuId:currentMenuId});
  }
}]);

home.controller("loginCtrl", ['$scope', function ($scope) {
  $.backstretch([
    "images/login/1.jpg",
    "images/login/2.jpg",
    "images/login/3.jpg",
    "images/login/4.jpg"
  ], {
    fade: 1000,
    duration: 8000
  });
}]);

home.controller("loginFormCtrl", ['$scope', '$http', 'loginService', '$state', 'i18n', 'commonService',
  function ($scope, $http, loginService, $state, i18n, commonService) {
    $scope.user = {
      username: '',
      password: '',
      rememberMe: false
    };
    $scope.user.username = 'demo@infitecs.com';
    $scope.user.password = 'Aa123456';

    $('#username').keypress(function (e) {
      if (e.which == 13) {
        $("#password").focus();
        return false;
      }
    }).focus();

    $scope.login = function () {
      commonService.blockUI("body");
      $scope.user.password = $scope.user.password.md5();
      $http.post(basePath + 'login.json', $.param($scope.user), {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
        .success(function (data) {
          Metronic.unblockUI(window.blockUITarget);
          var msg = i18n.translate(data.returnMsg);
          if (data.error) {
            toast.error(msg);
            $scope.user.password = '';
            return;
          }
          loginService.login(data.returnData.user);
          $state.go('index.dashboard');
        });
    };

    $('#password').keypress(function (e) {
      if (e.which == 13) {
        $scope.login();
        return false;
      }
    });

  }]);
